package org.devf.fork.imsimulator.http;

import org.devf.fork.imsimulator.http.listener.LogListener;
import org.devf.fork.imsimulator.service.TokenMgr;

import java.util.List;
import java.util.Map;

/**
 * ApiClientFactory related
 *
 * @author wanghuafu
 * @since 1.0  2023/9/13 17:21
 */
public class ApiClientFactory {

    private static final DefaultHttpClient defaultHttpClient = new DefaultHttpClient();
    private static ImageFileHttpClient imageFileHttpClient;

    public static synchronized DefaultHttpClient getDefaultClient() {
        defaultHttpClient.setHttpListenerList(List.of(
                new LogListener()
        ));
        if (TokenMgr.isLogin()) {
            defaultHttpClient.setHeaderMap(Map.of("token", TokenMgr.token));
        }
        return defaultHttpClient;
    }

    public static synchronized ImageFileHttpClient getImageHttpClient() {
        if (null == imageFileHttpClient) {
            imageFileHttpClient = new ImageFileHttpClient();
            imageFileHttpClient.setHttpListenerList(List.of(
                    new LogListener()
            ));
        }
        if (TokenMgr.isLogin()) {
            imageFileHttpClient.setHeaderMap(Map.of("token", TokenMgr.token));
        }
        return imageFileHttpClient;
    }


}
